import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '../views/Home.vue'
import store from  "../store/index"
import {userInfo} from "../api/api";
import index from '../layout/index'
Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'index',
    redirect:'home',
    component: index,
    children:[
      {
        path:'',
        component:home,
      },
      {
      path: '/music',
      name: 'About',
      component: resolve => (require([/* webpackChunkName: "about" */ '../views/music.vue'],resolve))
    },{
      path: '/mine',
      name: 'mine',
      component: () => import(/* webpackChunkName: "about" */ '../views/mine.vue')
    }

    ]
  },
  {
    path:'/login',
    component: () => import('../views/login.vue')
  },
  {
    path:'/newsList',
    component: () => import('../views/newsList')
  },
  {
    path:'/newsDetail',
    component: () => import('../views/newsDetail')},
  {
    path:'/forgetpassword',
    component: () => import('../views/forgetpassword')
  },
  {
    path:'/register',
    component: () => import('../views/register')
  },
  {
    path:'/minemessage',
    component: () => import('../views/minemessage')
  },
  {
    path:'/changemine',
    component: () => import('../views/changemine')
  },
  {
    path:'/newsList',
    component: () => import('../views/newsList')
  },
  {
    path:'/myCollection',
    component: () => import('../views/myCollection')
  },

  {
    path: '/search',
    component: () => import('../views/search')
  },
  {
    path:'/',
    component: () => import('../views/bot')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
let info = userInfo
//路由守卫
router.beforeEach((to, from, next) => {
  if(to.meta.login){
    info().then((res)=>{
      // console.log(res)
      if(res.code==0){
        store.commit("login",true)
        next()
      }else {
        next('/login')
      }
    })
  }else {
    info().then((res)=>{
      // console.log(res)
      if(res.code==0){
        store.commit("login",true)
        next();
      }else {
        store.commit("login",false)
        next();
      }

    })

  }
})
export default router
